#include "stdafx.h"
#include "Spectrum_XT239.h"
#include "log.h"


DWORD WINAPI ReadXT239ComData(void *pContext)
{
	CSpectrumXT239 *pDlg = (CSpectrumXT239 *)pContext;
	pDlg->DealXT239Data();
	return 0;
}


CSpectrumXT239::CSpectrumXT239()
{
}

CSpectrumXT239::~CSpectrumXT239()
{
}

BOOL CSpectrumXT239::init(int port)
{
	log_printf("CSpectrumXT239::init serial port=%d \n", port);
	mSerialPort = port;

	mComXT239 = new CComInterFace();
	mComXT239->InitReadThread();
	mTxHRecvThread = CreateThread(NULL, 0, ReadXT239ComData, this, 0, &mTxReadThreadID);
	OpenXT239Com();

	return TRUE;
}

BOOL CSpectrumXT239::destroy()
{
	m_exit_cmd_therad = TRUE;
	Sleep(500);
	CloseXT239Com();

	mComXT239->ExitReadThread();
	if (mTxHRecvThread != NULL)
	{
		CloseHandle(mTxHRecvThread);
	}

	log_printf("CSpectrumXT239::destroy() m_exit_cmd_therad=%d \n", m_exit_cmd_therad);
	return TRUE;
}

void CSpectrumXT239::OpenXT239Com()
{
	if (mComXT239->OpenPort(mSerialPort, 9600))
	{
		m_bComOpen = TRUE;
		log_printf("OpenCom() succeed nPort=%d \n", mSerialPort);
	}
	else
	{
		log_printf("OpenCom() XT239 failed nPort=%d \n", mSerialPort);
		m_bComOpen = FALSE;
		CString errorMsg;
		errorMsg.Format("Open XT239 Com(%d) failed, check it", mSerialPort);
		AfxMessageBox(errorMsg);
	}
}

void CSpectrumXT239::CloseXT239Com()
{
	if (m_bComOpen)
	{
		m_bComOpen = FALSE;
		mComXT239->ClosePort();
		log_printf("OpenCom() XT239 call mComXT239->ClosePort() \n");
	}
}


void CSpectrumXT239::SendDataToXT239(char *pData)
{
	log_printf("SendDataToXT239() = %s \n", pData);
	BYTE buf[512];
	memset(buf, 0, 512);
	int nlen = (int)strlen(pData);
	memcpy(buf, pData, nlen);
	buf[nlen] = 0x0D;
	buf[nlen + 1] = 0x0A;
	mComXT239->Send(buf, nlen + 2);
}

void CSpectrumXT239::DealXT239Data()
{
	BYTE buf[512];
	BYTE bufTmp[2];
	int index = 0;
	m_exit_cmd_therad = FALSE;
	while (!m_exit_cmd_therad)
	{
		//log_printf("m_bComOpen = %d m_exit_cmd_therad=%d \n",m_bComOpen, m_exit_cmd_therad);
		if (m_bComOpen)
		{
			memset(buf, 0, 512);
			if (mComXT239->GetDataCnt() < 2)
			{
				Sleep(500);
				continue;
			}

			index = 0;
			while (TRUE)
			{
				if (!m_bComOpen)
				{
					break;
				}
				//log_printf("pCom->GetDataCnt() = %d \n",pCom->GetDataCnt());
				if (mComXT239->GetDataCnt() >= 1)
				{
					mComXT239->Read(bufTmp, 1);
					buf[index] = bufTmp[0];
					index++;

					//log_printf("buf[%d] = %02x \n",index-1, buf[index-1]);
					//log_printf("buf[%d] = %c \n",index-1, buf[index-1]);
					if (bufTmp[0] == 0x0A || index >= 500)
					{
						CString temp(buf);
						log_printf("Recv XT239 = %s \n", (char *)(LPCTSTR)temp);
						if (temp.GetLength()>=1)
						{
							mXT239DataCallback(mPOwner, temp);
						}
						break;
					}
				}
				else
				{
					Sleep(500);
				}
			}
		}
		else
		{
			Sleep(2000);
		}
	} //end while(TRUE)
}


void CSpectrumXT239::XT239SetDataCallback(void *pOwner, int (CALLBACK* fSetDataCallback)(void *, CString))
{
	mPOwner = pOwner;
	mXT239DataCallback = fSetDataCallback;
}

CString CSpectrumXT239::GetParseMaxFrequency(CString value)
{
	//MAX:2410.10MHz, -34.4dBm
	CString result;
	result = mCommonUtils.GetRightString(value, ":");
	log_printf("CSpectrumXT239->GetParseMaxFrequency() 11= %s \n",result);
	result = mCommonUtils.GetLeftString(result, "MHz");
	log_printf("CSpectrumXT239->GetParseMaxFrequency() 22= %s \n",result);
	return result;
}

CString CSpectrumXT239::GetParseMaxSNR(CString value)
{
	//MAX:2410.10MHz, -34.4dBm
	CString result;
	result = mCommonUtils.GetRightString(value, ",");
	log_printf("CSpectrumXT239->GetParseMaxSNR() 11= %s \n",result);
	result = mCommonUtils.GetLeftString(result, "dBm");
	log_printf("CSpectrumXT239->GetParseMaxSNR() 22= %s \n",result);
	return result;
}

void CSpectrumXT239::SetEnterRealFreqInterface()
{
	SendDataToXT239(XT239_SET_REAL_FREQ_INTERFACE);
}